package src 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author Felipe Soares Pinhel
	 */
	public class Game extends Sprite
	{
		private var scene	 : String;
		private var intro	 : Intro;
		private var menu	 : Menu;
		private var level	 : Level;
		private var gameOver : GameOver;
		
		private var count : int;
		private var boolFadeOut : Boolean;
		
		public function Game() 
		{
			trace("GAME");
			
			init();
			this.addEventListener(Event.ENTER_FRAME, update,false,0,true);
			
		}
		
		private function init() : void
		{
			//===============================================================================Shortcut
			count = 0;
			cScene("INTRO");
		}
		
		private function cScene(_scene : String) : void
		{
			//this.scene = _scene;
			
			trash();
						
			switch(_scene)
			{
				case "INTRO" :
					trace("________INTRO");
					intro = new Intro();
					this.addChild(intro);
					intro.alpha = 0;
					intro.x = Main._instance.stage.stageWidth/2;
					intro.y = Main._instance.stage.stageHeight/2;
					
				break;
				
				case "MENU" :
					trace("________MENU");
					menu = new Menu()
					this.addChild(menu); 	
					menu.alpha = 0;
					menu._btnStart.addEventListener(MouseEvent.CLICK, clickStart, false, 0, true);
				break;
				
				case "LEVEL" :
					trace("________LEVEL");
					level = new Level();
					this.addChild(level);
				break;
				
				case "GAMEOVER" :
					trace("________GAMEOVER");
					gameOver = new GameOver();
					this.addChild(gameOver);
				break;

			}
			
		}
		
		private function trash() : void
		{
			trace("_TRASH_")
			if ( intro != null )
			{
				this.removeChild(intro);
				intro = null;
				
			}
			else if ( menu != null )
			{
				menu._btnStart.removeEventListener(MouseEvent.CLICK, clickStart);
				this.removeChild(menu);
				menu = null;
				
			}
			else if ( level != null )
			{
				this.removeChild(level);
				level = null;
				
			}
			else if ( gameOver != null )
			{
				//this.removeChild(gameOver);
				//gameOver = null;
				
			}
			
			
		}
		
		//=========================================================================================================UPDATE
		private function update( e : Event ) : void
		{
			
			
			
			if (intro != null)
			{
				count ++
				
				if (count <= 60)
				{
					intro.alpha += 0.0175;
				}
				if (count >= 60 && count <= 90)
				{
					intro.alpha -= 0.0175;
				}
				if (count >= 90)
				{
					count = 0;
					cScene("MENU");
				}
			}
			
			if (menu != null)
			{
				if (menu.alpha <= 1)
				{
					menu.alpha += 0.0175;
				}
			}
			
			if (level != null)
			{
				if (level.alpha <= 1)
				{
					//menu.alpha += 0.03;
					
					level.update();
				}
			}
			
			if (boolFadeOut)
			{
				if (menu.alpha > 0)
				{
					menu.alpha -= 0.3;
				}
				else
				{	
					cScene("LEVEL");
					boolFadeOut = false;
				}
			}
		}
			
		private function clickStart ( e : MouseEvent ) : void
		{
			boolFadeOut = true;
		}
		
		
		private function clickOptions ( e : MouseEvent ) : void
		{
			
		}
		
		
		
		
		
		
	}

}